package com.glowdayz.glowmee.bc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.glowmee.cmd.HotEditerCMD;
import com.glowdayz.glowmee.dao.HotEditerDAO;

public class UpdateEditerProductBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public UpdateEditerProductBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String hotEditerType = request.getParameter("type");
		int idProduct = Integer.parseInt(request.getParameter("pId"));
		int idSecondCategory = Integer.parseInt(request.getParameter("sId"));
		
		try {
			HotEditerCMD cmd = HotEditerDAO.getInstance().getHotEditer(idProduct, idSecondCategory, hotEditerType);
			request.setAttribute("cmd", cmd);
			request.setAttribute("pId", idProduct);
			request.setAttribute("sId", idSecondCategory);
			request.setAttribute("type", hotEditerType);
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/product/updateEditerProduct.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String hotEditerType = request.getParameter("type");
		int idProduct = Integer.parseInt(request.getParameter("pId"));
		int idSecondCategory = Integer.parseInt(request.getParameter("sId"));
		
		int isDisplay = Integer.parseInt(request.getParameter("isDisplay"));
		int isTop = Integer.parseInt(request.getParameter("isTop"));
		String promotionText = request.getParameter("promotionText");
		String start_date = request.getParameter("start_date");
		start_date = start_date.replace("-", "") + "000000";
		String end_date = request.getParameter("end_date");
		end_date = end_date.replace("-", "") + "235959";
		
		try {
			HotEditerDAO.getInstance().updateHotEditer(new HotEditerCMD(
					idProduct, idSecondCategory, "EDITER", promotionText, isTop, isDisplay, start_date, end_date));
			request.setAttribute("cmd", HotEditerDAO.getInstance().getHotEditer(idProduct, idSecondCategory, hotEditerType));
			request.setAttribute("alert", "<script type='text/javascript'>alert('수정이 완료되었습니다.')</script>");
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/product/updateEditerProduct.jsp");
		dpr.forward(request, response);
	}

}